//	Pixlib
import com.bourre.data.collections.Map;
import com.bourre.events.EventType;
import com.bourre.events.IEventDispatcher;
import com.bourre.log.Logger;
import com.bourre.log.LogLevel;

//	Oregano
import org.omus.msg.Message;
import org.omus.msg.Messenger;
import org.omus.util._Error;
import org.omus.core.User;

//	Pixlib API for Oregano
import net.webbymx.pixgano.events.MessageEvent;
import net.webbymx.pixgano.handlers.message.MessageType;


class net.webbymx.pixgano.handlers.message.MessageHandler {
/* ****************************************************************************
* PRIVATE STATIC VARIABLES
**************************************************************************** */
	private static var _oI			: MessageHandler;	
	
/* ****************************************************************************
* PRIVATE VARIABLES
**************************************************************************** */
	private var _mE					: Map;
	private var _oEB				: IEventDispatcher;
	
/* ****************************************************************************
* CONSTRUCTOR
**************************************************************************** */
	private function MessageHandler( oEB : IEventDispatcher ) 
	{
		_mE = new Map();
		_oEB = oEB;
		Messenger.getInstance().addListener( this );
	}
	
/**
* Singleton access
*/	
	public static function getInstance( oEB : IEventDispatcher ) : MessageHandler  
	{
		if ( !_oI ) 
		{
			if ( oEB == undefined ||  ! ( oEB instanceof IEventDispatcher ) ) 
			{
				Logger.LOG( "Cannot create the MessageHandler object. You need to pass an EventBroadcaster as a parameter", LogLevel.FATAL );
				throw( "Cannot create the MessageHandler object. You need to pass an EventBroadcaster as a parameter" );
			}
			else {
				_oI = new MessageHandler( oEB );
			}
		}
		return _oI; 
	}


/* ****************************************************************************
* PRIVATE FUNCTIONS
**************************************************************************** */
	private function _fireEvent( e : MessageEvent ) : Void 
	{
		_oEB.broadcastEvent( e );
	}

	
/* ****************************************************************************
* PUBLIC FUNCTIONS
**************************************************************************** */
	public function registerMessage( msg : MessageType, event : EventType ) : Void 
	{
//		Logger.LOG( "MessageHandler :: registerMessage, args : " + msg + ", " + event, LogLevel.INFO );
		if ( _mE.containsKey( msg ) ) Logger.LOG( "A Message  '" + msg + "' already exist. It will be replaced by the new one", LogLevel.WARN );
		_mE.put( msg.toString(), event );
	}


	public function onError( error : _Error ) : Void {}


/**
* call back from the server 
* will call the fireEvent method passing the event associated with the message received
* @param	msg
*/
	public function onMessage( msg:Message ) : Void 
	{
		Logger.LOG( "MessageHandler :: onMessage : " + msg.getSubject(), LogLevel.INFO );
		Logger.LOG( msg );
	//	if we have registered an event for this message
		if ( _mE.containsKey( msg.getSubject() ) ) 
		{
//			Logger.LOG( "MessageHandler :: existing message with subject : "+msg.getSubject(), LogLevel.INFO );			
		//	create a new MessageEvent with the appropriate event
			var msgEvt : MessageEvent = new MessageEvent( _mE.get( msg.getSubject() ) );
//			Logger.LOG( "MessageHandler :: corresponding message : "+msgEvt, LogLevel.INFO );
		//	attach the Message
			msgEvt.setMessage( msg );
		//	fire the event
			_fireEvent( msgEvt );
		}
		else
		{
			Logger.LOG( "MessageHandler :: Unknown message "+msg+" :: filtered !", LogLevel.INFO );
		}
	}
	
	public function onSubscribe( subject : String ) : Void {}
	public function onUnsubscribe( subject : String ) : Void {}
	public function onUnsubscribeAll() : Void {}
}
